Passed
Push — master ( 7811b9...4b84d8 )
by Leon
01:44
created

meta-box-fields.js ➔ jQuery   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 99

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 1 Features 0
Metric Value
cc 1
c 7
b 1
f 0
nc 1
nop 1
dl 0
loc 99
rs 8.3103

6 Functions

Rating   Name   Duplication   Size   Complexity  
A meta-box-fields.js ➔ ... ➔ cmb2MetaBoxField.fieldTypeChange 0 6 1
A meta-box-fields.js ➔ ... ➔ cmb2MetaBoxField.init 0 6 1
A meta-box-fields.js ➔ ... ➔ cmb2MetaBoxField.fieldNameChange 0 5 1
A meta-box-fields.js ➔ ... ➔ cmb2MetaBoxField.showHideOnLoad 0 7 1
A meta-box-fields.js ➔ ... ➔ cmb2MetaBoxField.showHide 0 6 1
A meta-box-fields.js ➔ ... ➔ cmb2MetaBoxField.displayUsageFunctions 0 9 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
jQuery( function( $ ) {
2
3
	/**
4
	 * JS meta_box field actions.
5
	 */
6
	var cmb2MetaBoxField = {
7
8
		/**
9
		 * Initialize meta box fields behavior.
10
		 */
11
		init: function() {
12
            this.showHideOnLoad();
13
			this.fieldTypeChange();
14
			this.fieldNameChange();
15
			this.displayUsageFunctions();
16
		},
17
18
		/**
19
		 * Show / hide field rows based on field_type_select selected value.
20
		 *
21
		 * showHide
22
		 *
23
		 *
24
		 *
25
		 */
26
		showHide: function( fieldTypeSelect ) {
27
            var fieldTypeVal = $( fieldTypeSelect ).val();
28
            var fieldSet     = $( fieldTypeSelect ).closest( '.cmb-field-list' );
29
            $( '.cmb-row.cmb_hide_field', fieldSet ).hide();
30
            $( '.cmb-row.' + fieldTypeVal, fieldSet ).show();
31
		},
32
33
		/**
34
		 * Use the showHide() function on each field group's field rows on page load.
35
		 *
36
		 * showHideOnLoad
37
		 *
38
		 *
39
		 *
40
		 */
41
		showHideOnLoad: function() {
42
            var fieldTypeSelects = $( '.field_type_select' );
43
            fieldTypeSelects.each( function() {
44
                cmb2MetaBoxField.showHide( this );
45
            } );
46
47
		},
48
49
		/**
50
		 * Use the showHide() function on this field group's field rows on field_type_select change.
51
		 *
52
		 * fieldTypeChange
53
		 *
54
		 *
55
		 *
56
		 */
57
		fieldTypeChange: function() {
58
            $( '.cmb2-metabox' ).on( 'change', '.field_type_select', function( e ) {
59
                var fieldTypeSelect = e.target;
60
                cmb2MetaBoxField.showHide( fieldTypeSelect );
61
            } );
62
		},
63
64
		/**
65
		 * Update the usage code snippets when field_name changes.
66
		 *
67
		 * fieldNameChange
68
		 *
69
		 *
70
		 *
71
		 */
72
		fieldNameChange: function() {
73
            $( '.cmb2-metabox' ).on( 'change keyup', '.field_name', function( e ) {
0 ignored issues
show
Unused Code introduced by
The parameter e is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
74
                this.displayUsageFunctions();
75
            }.bind( this ) );
76
		},
77
78
		/**
79
		 * Convert field name to usage code snippet.
80
		 *
81
		 * displayUsageFunctions
82
		 *
83
		 *
84
		 *
85
		 */
86
		displayUsageFunctions: function() {
87
			var fieldName = $( '.field_name' );
88
			var wrapper = fieldName.closest( '.cmb-field-list' );
89
			var getPostMeta = $( '.get_post_meta', wrapper );
90
			var cmbf = $( '.cmbf', wrapper );
91
			var fieldNameVal = fieldName.val().toLowerCase().replace( / /g, '_' );
92
			getPostMeta.val( 'get_post_meta( get_the_ID(), \'_' + fieldNameVal + '\', true );');
93
			cmbf.val( 'cmbf( get_the_ID(), \'_' + fieldNameVal + '\' );');
94
		}
95
	};
96
97
	cmb2MetaBoxField.init();
98
99
} );
100